import Cookies from 'js-cookie'

const state = {
  sidebar: {
    locked: Cookies.get('sidebarLocked') ? !!+Cookies.get('sidebarLocked') : true,
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  navbar:{
    opened: Cookies.get('navbarStatus') ? !!+Cookies.get('navbarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  size: Cookies.get('size') || 'medium'
}

const mutations = {
  SET_SIDEBAR_CLOSE:state=>{
    state.sidebar.opened = false;
    Cookies.set('sidebarStatus', 0);
  },
  SET_SIDEBAR_OPEN:state=>{
    state.sidebar.opened = true;
    Cookies.set('sidebarStatus', 1);
  },
  SET_SIDEBAR_UNLOCK:state=>{
    state.sidebar.locked = false;
    Cookies.set('sidebarLocked', 0);
  },
  SET_SIDEBAR_LOCK:state=>{
    state.sidebar.locked = true;
    Cookies.set('sidebarLocked', 1);
  },
  TOGGLE_SIDEBAR: (state,param) => {
    if((param && param.flag=='lock')){
      state.sidebar.locked = !state.sidebar.locked
      if(state.sidebar.locked){
        state.sidebar.opened = true;
      }else{
        state.sidebar.opened = !state.sidebar.opened
      }
    }else{
      if(state.sidebar.locked){
        state.sidebar.opened = true;
      }else{
        state.sidebar.opened = !state.sidebar.opened
        state.sidebar.withoutAnimation = false
        if(param && param.flag=='1'){
          state.sidebar.opened = true;
        }
      }
    }
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
    if (state.sidebar.locked) {
      Cookies.set('sidebarLocked', 1)
    } else {
      Cookies.set('sidebarLocked', 0)
    }
  },
  TOGGLE_NAVBAR: state => {
    state.navbar.opened = !state.navbar.opened
    state.navbar.withoutAnimation = false
    if (state.navbar.opened) {
      Cookies.set('navbarStatus', 1)
    } else {
      Cookies.set('navbarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_SIZE: (state, size) => {
    state.size = size
    Cookies.set('size', size)
  }
}

const actions = {
  setSideBarClose({ commit }) {
    commit('SET_SIDEBAR_CLOSE')
  },
  setSideBarOpen({ commit }) {
    commit('SET_SIDEBAR_OPEN')
  },
  setSideBarUnLock({ commit }) {
    commit('SET_SIDEBAR_UNLOCK')
  },
  setSideBarLock({ commit }) {
    commit('SET_SIDEBAR_LOCK')
  },
  toggleSideBar({ commit },param) {
    commit('TOGGLE_SIDEBAR',param)
  },
  toggleNavBar({ commit }) {
    commit('TOGGLE_NAVBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
